package com.tbgov.controller;

import com.tbgov.entity.vo.UploadFile;
import com.tbgov.service.SftpUploadService;
import com.tbgov.util.Base64ToMultipartFileUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author 1st
 */

@Tag(name = "文件上传管理", description = "文件上传相关接口")
@RestController
@RequestMapping("file/")
@RequiredArgsConstructor
public class UploadController {

    final SftpUploadService sftpUploadService;

    /*@Operation(summary = "上传图片")
    @PostMapping(value = "uploadImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<String> uploadImage(@RequestParam MultipartFile file) {
        return ResponseEntity.ok(sftpUploadService.uploadFile(file));
    }*/

    @Operation(summary = "上传图片")
    @PostMapping(value = "uploadImage")
    public ResponseEntity<String> uploadImage(@Valid @RequestBody UploadFile file) {
        try {
            return ResponseEntity.ok(sftpUploadService.uploadFile(Base64ToMultipartFileUtil.base64ToMultipartFile(file.getBase64())));
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

    @Operation(summary = "删除图片")
    @DeleteMapping("delImage")
    ResponseEntity<Object> deleteImage(@RequestParam("fileName") String fileName) {
        Assert.isTrue(StringUtils.isNotBlank(fileName), "文件名不能为空");
        sftpUploadService.deleteFile(fileName);
        return ResponseEntity.ok("success");
    }

}
